package org.apereo.cas.config;

import com.sso.auth.MyAuthenticationHandler;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlan;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlanConfigurer;
import org.apereo.cas.authentication.AuthenticationHandler;
import org.apereo.cas.authentication.principal.DefaultPrincipalFactory;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.services.ServicesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类 {@code CustomLoginCredentialConfiguration}  <br> 定制登录配置.
 *
 * <p>详细描述
 * <p>
 *
 * @author <a href="mailto:lz2392504@gmail.com">CN華少</a>
 * @see AuthenticationEventExecutionPlanConfigurer
 * @since v1.0.0
 */
@Configuration("customLoginCredentialConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class CustomLoginCredentialConfiguration implements AuthenticationEventExecutionPlanConfigurer {

    @Autowired
    private CasConfigurationProperties casConfigurationProperties;

    @Autowired
    @Qualifier("servicesManager")
    private ServicesManager servicesManager;


    /**
     * 调用自定义验证逻辑
     * @return
     */
    @Bean
    public AuthenticationHandler myAuthenticationHandler(){
        return new MyAuthenticationHandler(MyAuthenticationHandler.class.getName(),
                servicesManager, new DefaultPrincipalFactory(), 1
        );
    }

    /**
     * 使用自定义的handler进行验证工作
     * @param plan
     */
    @Override
    public void configureAuthenticationExecutionPlan(AuthenticationEventExecutionPlan plan) {
        plan.registerAuthenticationHandler(myAuthenticationHandler());
    }

    @Override
    public String getName() {
        return AuthenticationEventExecutionPlanConfigurer.super.getName();
    }

    @Override
    public int getOrder() {
        return AuthenticationEventExecutionPlanConfigurer.super.getOrder();
    }
}
